package org.zn.mvcbase.exception;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;

/**
 * @ProjectName:zqdk-mvcbase
 * @Description: 
 * @author: 周楠
 * @date 2017年3月6日 上午10:33:34
 */

public abstract class AbstractResponseHandlerState implements ExceptionResponseState  {

	protected List<MediaType> acceptedMediaTypes;  
	protected HttpOutputMessage outputMessage ; 
	protected HttpInputMessage inputMessage ;
	protected HttpHeaders headersMap;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map<String, HttpMessageConverter<?>> converterMap;
	
	public void setContext(HttpServletRequest request,
			HttpServletResponse response,Map<String, HttpMessageConverter<?>> converterMap){
		
		this.request=request;
		this.response=response;
		this.converterMap=converterMap;
		this.inputMessage= new ServletServerHttpRequest(request);  
		this.headersMap=inputMessage.getHeaders();
		this.acceptedMediaTypes = headersMap.getAccept();
		this.outputMessage = new ServletServerHttpResponse(response);
		
		if (acceptedMediaTypes.isEmpty()) {  
            acceptedMediaTypes = Collections.singletonList(MediaType.ALL);  
        }  
		
	}
	
	
}
